package com.czzapp.xiaoyumemo.view;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.czzapp.xiaoyumemo.R;
import com.czzapp.xiaoyumemo.model.Memo;
import com.czzapp.xiaoyumemo.model.MemoManager;
import com.czzapp.xiaoyumemo.model.MemoManagerListener;
import com.czzapp.xiaoyumemo.model.Memo.MemoState;
import com.czzapp.xiaoyumemo.view.CurrentMemoTabActivity.ItemView;
import com.czzapp.xiaoyumemo.view.CurrentMemoTabActivity.myAdapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.format.DateUtils;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

//过期备忘界面
public class AgoMemoTabActivity extends Activity implements MemoManagerListener{
	private ListView mListView = null;
	private Button mButtonAdd = null;
	private List<Map<String, Object>> mMemoList = new ArrayList<Map<String, Object>>();
	private myAdapter mSimpleAdapter = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.ago_memo_tab);
		MemoManager.getInstance().addListener(this);
		mButtonAdd = (Button)findViewById(R.id.ago_memo_Add);                                                                                                                                                                                                                                                                                                                       
		mListView = (ListView)findViewById(R.id.ago_memo_list);
		//mSimpleAdapter = new SimpleAdapter(this,getData(),R.layout.list_item,new String[]{"title"},new int[]{R.id.title});
		mSimpleAdapter = new myAdapter(this,getData());
		mListView.setAdapter(mSimpleAdapter);
		mButtonAdd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				Intent it = new Intent(AgoMemoTabActivity.this, EditingMemoActivity.class);
				Bundle bundle=new Bundle();
				bundle.putString("name", "This is from MainActivity!");
				it.putExtras(bundle);      
				startActivity(it);
			}
		});
		//单击
		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				Memo memo = null;
				synchronized (mMemoList) {
					if(mMemoList.size() > arg2) {
						memo = (Memo)(mMemoList.get(arg2).get("Memo"));
					}
				}
				if(memo != null) {
					Intent it = new Intent(AgoMemoTabActivity.this, EditingMemoActivity.class);
					Bundle bundle=new Bundle();
					bundle.putInt("MemoId", ((Memo)(mMemoList.get(arg2).get("Memo"))).getId());
					it.putExtras(bundle);      
					startActivity(it);
				}
			}
		});
		//添加长按点击  
        mListView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
			
			@Override
			public void onCreateContextMenu(ContextMenu menu, View v,
					ContextMenuInfo menuInfo) {
				
				menu.setHeaderTitle("操作");     
                menu.add(0, 0, 0, "编辑");  
                menu.add(0, 1, 0, "删除");                      
			}
		});      
	}
	//长按菜单响应函数  
		@Override
		public boolean onContextItemSelected(MenuItem item) {
			AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
			int pos = info.position;
			if(item.getItemId() == 0)
			{
				Intent it = new Intent(AgoMemoTabActivity.this, EditingMemoActivity.class);
				Bundle bundle=new Bundle();
				bundle.putInt("MemoId", ((Memo)(mMemoList.get(pos).get("Memo"))).getId());
				it.putExtras(bundle);      
				startActivity(it);
			}
			else if(item.getItemId() == 1)
			{
				final int poss = pos;
				String title = (String) mMemoList.get(pos).get("title");
				new AlertDialog.Builder(this)
				.setTitle("您确定要删除" + title + "吗？")
				.setPositiveButton("确定",new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						MemoManager.getInstance().deleteMemo((Memo)mMemoList.get(poss).get("Memo"));
					}
				})
				.setNegativeButton("取消",new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						
					}
				}).show();
				
			}
			return super.onContextItemSelected(item);
		}
	private List<Map<String, Object>> getData(){
		synchronized (mMemoList) {
			mMemoList.clear();	        
			List<Memo> agoMemoList = MemoManager.getInstance().getGoneMemoList();		
	       for(int i = 0; i < agoMemoList.size();i++)
	       {
	    	   Map<String, Object> map = new HashMap<String, Object>();
	    	   map.put("title", agoMemoList.get(i).getTitle());
		       map.put("Memo", agoMemoList.get(i));
		       mMemoList.add(map);
	       }
		}
       return mMemoList;
	}
	@Override
	public void onComingMemoListChanged() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onMissedMemoListChanged() {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onGoneMemoListChanged() {
		getData();
		mSimpleAdapter.notifyDataSetChanged();	
	}
	public class myAdapter extends BaseAdapter{
		private Context mContext; 
		 private LayoutInflater ViewContainer; 
		private List<Map<String, Object>> mData;
		public myAdapter(Context context, List<Map<String, Object>> data)
		{
			this.mContext = context;  
			mData = data;
			ViewContainer = LayoutInflater.from(context); 
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return mData.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return mData.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View convertView, ViewGroup arg2) {
			// TODO Auto-generated method stub
			ItemView itemview = null;
			if(convertView == null)
			{
				convertView = ViewContainer.inflate(R.layout.list_item, null);
				itemview = new ItemView();
				itemview.mTextTitle = (TextView)convertView.findViewById(R.id.title);
				itemview.mTextDate = (TextView)convertView.findViewById(R.id.date);
				itemview.mTextTime = (TextView)convertView.findViewById(R.id.time);
				itemview.mTextTitleNote = (TextView)convertView.findViewById(R.id.titleNote);
				convertView.setTag(itemview);  
			}
			else
			{
				itemview = (ItemView)convertView.getTag();
				
			}
			String title = (String)mData.get(arg0).get("title");
			itemview.mTextTitle.setText(title);
			itemview.mTextTitleNote.setVisibility(View.GONE);
			Memo memo = (Memo)mData.get(arg0).get("Memo");
			if(memo != null)
			{
				Date date = memo.getDelayDate();				
				String dateStr = (String)DateUtils.formatDateTime(mContext, date.getTime(), DateUtils.FORMAT_SHOW_DATE);
				String weekStr = (String)DateUtils.formatDateTime(mContext, date.getTime(), DateUtils.FORMAT_SHOW_WEEKDAY);	
				itemview.mTextDate.setText(dateStr + " " + weekStr);
				
				String timeStr = (String)DateUtils.formatDateTime(mContext, date.getTime(), DateUtils.FORMAT_SHOW_TIME);
				itemview.mTextTime.setText(timeStr);
				if(memo.getState() == MemoState.Missed)
				{
					itemview.mTextTitleNote.setVisibility(View.VISIBLE);
					Toast.makeText(mContext, "您有逾期的备忘", Toast.LENGTH_SHORT);
				}
			}
			
			return convertView;
		}		
	}
	public class ItemView
	{
		public TextView mTextTitle;
		public TextView mTextTitleNote;
		public TextView mTextDate;
		public TextView mTextTime;
	}
}
